package com.xsy.android.compose.wandroid.ui.page.home

import androidx.lifecycle.MutableLiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.xsy.android.compose.wandroid.logic.base.paging.HomePagingSource
import com.xsy.android.compose.wandroid.logic.base.repository.BaseArticlePagingRepository
import com.xsy.android.compose.wandroid.logic.model.BannerBean
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.model.PlayError
import com.xsy.android.compose.wandroid.logic.model.PlayLoading
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.logic.model.PlaySuccess
import com.xsy.android.compose.wandroid.logic.model.Query
import com.xsy.android.compose.wandroid.logic.network.Network
import kotlinx.coroutines.flow.Flow

/**
 *@author: xiaosy
 *时间: 2024/7/8
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class HomeArticlePagingRepository : BaseArticlePagingRepository() {
    override fun getPagingData(query: Query): Flow<PagingData<ArticleModel>> = Pager(
        PagingConfig(
            pageSize = PAGE_SIZE,
            enablePlaceholders = false
        )
    ) {
        HomePagingSource()
    }.flow

    suspend fun getBanner(state: MutableLiveData<PlayState<List<BannerBean>>>) {
        state.postValue(PlayLoading)
        val bannerResponse = Network.getBanner()
        if(bannerResponse.errorCode == 0){
            val bannerList = bannerResponse.data
            bannerList.forEach{
                it.data = it.imagePath
            }
            state.postValue(PlaySuccess(bannerList))
        }else{
            state.postValue(PlayError(RuntimeException("response status is ${bannerResponse.errorCode} msg is ${bannerResponse.errorMsg}")))
        }
    }
}